package org.example.codetrail.service.impl;

import org.example.codetrail.mapper.RepoMapper;
import org.example.codetrail.pojo.Gitee.GiteeRepoContent;
import org.example.codetrail.pojo.Repository;
import org.example.codetrail.pojo.Result;
import org.example.codetrail.service.RepoService;
import org.example.codetrail.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class RepoServiceImpl implements RepoService {
    @Autowired
    private RepoMapper repoMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Repository findByHtmlUrl(String htmlUrl) {
        return repoMapper.findByHtmlUrl(htmlUrl);
    }

    @Override
    public void register(String htmlUrl,String full_name) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        String accessToken=operations.get("accessToken");
        String platformName=operations.get("platformName");

        Repository repo = new Repository();
        repo.setUserId(id);
        repo.setRepositoryUrl(htmlUrl);
        String[] names = full_name.split("/");
        repo.setSourceAccount(names[0]);
        repo.setRepositoryName(names[1]);
        repo.setSourcePlatform(platformName);
        repo.setAccessToken(accessToken);

        repoMapper.register(repo);
    }

    @Override
    public List<Repository> getLocalRepo(Integer repositoryId) {
        return repoMapper.getLocalRepo(repositoryId);
    }

    @Override
    public Repository getLocalRepoInfo(Integer repositoryId) {
        return repoMapper.getLocalRepoInfo(repositoryId);
    }


}
